package stat

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"pqstat/pkg/dao"
	"pqstat/pkg/log"
)

type CateRank struct{}

func (cr CateRank) Invoke(msg interface{}) {
	data := msg.(map[string]interface{})
	log.Info(fmt.Sprintf("%0f", data["uid"].(float64)))
	o := orm.NewOrm()
	stat := &dao.PqUserStat{
		PqUserId:         int(data["uid"].(float64)),
		PqTestCategoryId: int(data["cid"].(float64)),
	}
	if created, _, err := o.ReadOrCreate(stat, "PqUserId", "PqTestCategoryId"); err == nil {
		if created {
			stat.Wrong = int(data["wrong"].(float64))
			stat.Right = int(data["right"].(float64))
			stat.Qc = int(data["qc"].(float64))
		} else {
			stat.Wrong += int(data["wrong"].(float64))
			stat.Right += int(data["right"].(float64))
			stat.Qc += int(data["qc"].(float64))
		}
		if _, err2 := o.Update(stat); err2 != nil {
			log.Error(err2.Error())
		}
	}
}
